<template>
	<div class="boxmain">
		<h3>生成内容</h3> 
		<ul class="switchBox">
			<li>
				<span>一科多版</span>
				<el-switch
				  v-model="ykdbswitch">
				</el-switch>
			</li>
			<li>
				<span>上线分析</span>
				<el-switch
				  v-model="onlineswitch"
				  @change = "onlineswitchButton">
				</el-switch>
			</li>
			<li>
				<span>赋分</span>
				<el-switch
				  v-model="ffswitch">
				</el-switch>
			</li>
		</ul>
		<h3 style="margin-top:20px;">上线信息 <span @click="fixecj">修改人数</span></h3> 
		<ul class="switchBox1">
			<li>
				<span>{{online.type3 == 2 ? '历史' : '文科'}}A线上报总人数</span>
				<el-input v-model="online.wknum1" disabled placeholder="请输入内容"></el-input>
			</li>
			<li>
				<span>{{online.type3 == 2 ? '历史' : '文科'}}B线上报总人数</span>
				<el-input v-model="online.wknum2" disabled placeholder="请输入内容"></el-input>
			</li>
			<li>
				<span>{{online.type3 == 2 ? '物理' : '理科'}}A线上报总人数</span>
				<el-input v-model="online.lknum1" disabled placeholder="请输入内容"></el-input>
			</li>
			<li>
				<span>{{online.type3 == 2 ? '物理' : '理科'}}B线上报总人数</span>
				<el-input v-model="online.lknum2" disabled placeholder="请输入内容"></el-input>
			</li>
		</ul>
		<h3>生成形式</h3> 
		<ul class="switchBox">
			<li>
				<span>成绩分析</span>
				<el-switch
				  v-model="cjfxswitch">
				</el-switch>
			</li>
			<li>
				<span>PDF分析</span>
				<el-switch
				  v-model="pdfswitch">
				</el-switch>
			</li>
			<li>
				<span>卷面留痕</span>
				<el-switch
				  v-model="youhenswitch">
				</el-switch>
			</li>
		</ul>
		<div class="startsc">
			<!-- getMarklist -->
			<el-button type="primary" @click="downloadFile(0)">开始生成</el-button>
			<!-- <el-button v-if="loadIshow">
				<div style="display: flex;align-items: center;">
					<img style="width: 20px;margin-right: 4px;" src="../../assets/image/loading1.gif" alt="">
					生成中
				</div>
			</el-button> -->
			<!-- <el-button type="primary" v-else @click="downloadFile(1)">重新生成</el-button> -->
		</div>
	</div>
</template>

<script>
	import {groupList,downloadExcel,markpaperafter,analysisStuScoreExcelDownloadPage,downloadPdfExamGenerate,downloadPdfPage,navmarkpaperlist,downloadprogressget} from '../../../api/scoreapi.js'
	export default{
		data(){
			return {
				ykdbswitch:true,
				onlineswitch:false,
				ffswitch:true,
				lanumber:'',
				lbnumber:'',
				wanumber:'',
				wbnumber:'',
				online:{},
				loadIshow:false,
				cjfxswitch:true,
				youhenswitch:false,
				pdfswitch:false,
				excelanalysis:[],
				pdfanalysis:[],
				markanalysis:[],
				progresstotal:'',
				pageIndex:1,
				pageSize:200,
				
			}
		},
		mounted() {
			this.getScreenHeight();
			//在窗口大小变化时更新屏幕高度，可以监听window的resize事件
			window.addEventListener('resize', this.getScreenHeight);
			let newtitle = decodeURIComponent(this.$route.query.con)
			this.titlecontent = JSON.parse(newtitle)
			this.onlinecontent()
			localStorage.setItem('menuState', 2);
			this.getjdlist()
			this.getExcelList()
			this.getPdflist()
			this.getMarklist()
		},
		methods:{
			getjdlist(){
				let paranms={
					navId:this.titlecontent.n5Id
				}
				downloadprogressget(paranms).then(res=>{
					console.info(res)
					if(res.code==0){
						this.progresstotal= res.data.toFixed(2)
					}
					
				})
			},
			getScreenHeight() {
			  // 获取屏幕高度并更新data中的screenHeight
			  this.tableheight = parseInt(window.innerHeight-450);
			},
			onlinecontent(){
				let params={
					navId:this.titlecontent.n5Id
				}
				groupList(params).then(res=>{
					if(res.code==0){
						this.online = res.data
						let allhe = parseInt(res.data.wknum1+res.data.wknum2+res.data.lknum1+res.data.lknum2)
						if(allhe>0){
							this.onlineswitch = true
						}
					}else{
						this.$message({
							type: 'error',
							message: res.msg
						}); 
					}
					
				})
			},
			//开始生成
			downloadFile(id) {
				// this.loadIshow = true
				//开始生成有痕
				// this.$message.success('查询进度或下载文件请进入生成相应页面查看')
				// 有痕和pdf
				console.info('dddd')
				if(this.youhenswitch&&this.pdfswitch&&this.cjfxswitch){
					console.info('PDF分析和生成成绩分析及有痕')
					if(this.excelanalysis.length<=0&&this.markanalysis.length<=0&&this.pdfanalysis.length<=0){
						this.$message.success('查询进度或下载文件请进入生成相应页面查看')
						this.downgetExamel(0)
						this.markdownContent()
						this.pdfDownContent()
					}
					if(this.markanalysis[0].generatedNum<this.markanalysis[0].num||this.progresstotal!=0&&this.excelanalysis[0].status==0||this.pdfanalysis[0].status!=0&&this.pdfanalysis[0].pdfNum<this.pdfanalysis[0].pdfTotal){
						this.$message.success('生成中，请勿重新点击，查询进度请前往下载成绩页面') 
						return
					}
					if(this.progresstotal==0&&this.excelanalysis[0].status==1||this.markanalysis[0].generatedNum==this.markanalysis[0].num||this.pdfanalysis[0].status==1&&this.pdfanalysis[0].pdfNum==this.pdfanalysis[0].pdfTotal){
						this.$confirm('已有分析包，是否从新生成?', '提示', {
						  confirmButtonText: '确定',
						  cancelButtonText: '取消',
						  type: 'warning'
						}).then(() => {
							this.$message.success('查询进度或下载文件请进入生成相应页面查看')
							this.downgetExamel(1)
							this.markdownContent()
							this.pdfDownContent()
						}).catch(() => {
						  this.$message({
							type: 'info',
							message: '已取消删除'
						  });          
						});
					}
				}else if(this.youhenswitch&&this.cjfxswitch){
					//有痕
					// console.info('有痕')
					console.info('有痕和生成成绩分析')
					if(this.excelanalysis.length<=0&&this.markanalysis.length<=0){
						this.$message.success('查询进度或下载文件请进入生成相应页面查看')
						this.downgetExamel(0)
						this.markdownContent()
					}
					// console.info('pdf和留痕')
					if(this.progresstotal!=0&&this.excelanalysis[0].status==0||this.markanalysis[0].generatedNum<this.markanalysis[0].num){
						this.$message.success('生成中，请勿重新点击，查询进度请前往下载成绩页面')
						return
					}
					if(this.progresstotal==0&&this.excelanalysis[0].status==1||this.markanalysis[0].generatedNum==this.markanalysis[0].num){
						this.$confirm('已有分析包，是否从新生成?', '提示', {
						  confirmButtonText: '确定',
						  cancelButtonText: '取消',
						  type: 'warning'
						}).then(() => {
							this.$message.success('查询进度或下载文件请进入生成相应页面查看')
							this.downgetExamel(1)
							this.markdownContent()
						}).catch(() => {
						  this.$message({
							type: 'info',
							message: '已取消删除'
						  });          
						});
					}
				}else if(this.youhenswitch&&this.pdfswitch){
					//有痕
					// console.info('有痕')
					console.info('有痕和pdf分析')
					if(this.pdfanalysis.length<=0&&this.markanalysis.length<=0){
						this.$message.success('查询进度或下载文件请进入生成相应页面查看')
						this.downgetExamel(0)
						this.markdownContent()
					}
					// console.info('pdf和留痕')
					if(this.pdfanalysis[0].status!=0&&this.pdfanalysis[0].pdfNum<this.pdfanalysis[0].pdfTotal||this.markanalysis[0].generatedNum<this.markanalysis[0].num){
						this.$message.success('生成中，请勿重新点击，查询进度请前往下载成绩页面')
						return
					}
					if(this.pdfanalysis[0].status==1&&this.pdfanalysis[0].pdfNum==this.pdfanalysis[0].pdfTotal||this.markanalysis[0].generatedNum==this.markanalysis[0].num){
						this.$confirm('已有分析包，是否从新生成?', '提示', {
						  confirmButtonText: '确定',
						  cancelButtonText: '取消',
						  type: 'warning'
						}).then(() => {
							this.$message.success('查询进度或下载文件请进入生成相应页面查看')
							this.pdfDownContent()
							this.markdownContent()
						}).catch(() => {
						  this.$message({
							type: 'info',
							message: '已取消删除'
						  });          
						});
					}
				}else if(this.pdfswitch&&this.cjfxswitch){
					console.info('PDF分析和生成成绩分析')
					if(this.excelanalysis.length<=0&&this.pdfanalysis.length<=0){
						this.$message.success('查询进度或下载文件请进入生成相应页面查看')
						this.downgetExamel(0)
						this.pdfDownContent()
					}
					// console.info('pdf和留痕')
					if(this.progresstotal!=0&&this.excelanalysis[0].status==0||this.pdfanalysis[0].status!=0&&this.pdfanalysis[0].pdfNum<this.pdfanalysis[0].pdfTotal){
						this.$message.success('生成中，请勿重新点击，查询进度请前往下载成绩页面')
						return
					}
					if(this.progresstotal==0&&this.excelanalysis[0].status==1||this.pdfanalysis[0].status==1&&this.pdfanalysis[0].pdfNum==this.pdfanalysis[0].pdfTotal){
						this.$confirm('已有分析包，是否从新生成?', '提示', {
						  confirmButtonText: '确定',
						  cancelButtonText: '取消',
						  type: 'warning'
						}).then(() => {
							this.$message.success('查询进度或下载文件请进入生成相应页面查看')
							this.downgetExamel(1)
							this.pdfDownContent()
						}).catch(() => {
						  this.$message({
							type: 'info',
							message: '已取消删除'
						  });          
						});
					}
				}else if(this.cjfxswitch){
					console.info('成绩分析')
					// this.downgetExamel(1)
					if(this.excelanalysis.length<=0){
						this.$message.success('查询进度或下载文件请进入生成相应页面查看')
						this.downgetExamel(0)
					}else{
						if(this.progresstotal!=0&&this.excelanalysis[0].status==0){
							this.$message.success('生成中，请勿重新点击，查询进度请前往下载成绩页面')
							return
						}
						if(this.progresstotal==0&&this.excelanalysis[0].status==1){
							this.$confirm('已有分析包，是否从新生成?', '提示', {
							  confirmButtonText: '确定',
							  cancelButtonText: '取消',
							  type: 'warning'
							}).then(() => {
								this.$message.success('查询进度或下载文件请进入生成相应页面查看')
								this.downgetExamel(1)
							}).catch(() => {
							  this.$message({
								type: 'info',
								message: '已取消删除'
							  });          
							});
						}
					}
				}else if(this.youhenswitch){
					console.info('有痕分析')
					console.info(this.markanalysis.length+'---')
					// generatedNum   num
					if(this.markanalysis.length<=0){
						this.$message.success('查询进度或下载文件请进入生成相应页面查看')
						this.markdownContent()
					}else{
						if(this.markanalysis[0].generatedNum<this.markanalysis[0].num){
							this.$message.success('生成中，请勿重新点击，查询进度请前往下载成绩页面')
							this.markdownContent()
							return
						}
						if(this.markanalysis[0].generatedNum==this.markanalysis[0].num){
							this.$confirm('已有有痕包，是否从新生成?', '提示', {
							  confirmButtonText: '确定',
							  cancelButtonText: '取消',
							  type: 'warning'
							}).then(() => {
								this.$message.success('查询进度或下载文件请进入生成相应页面查看')
								this.markdownContent()
							}).catch(() => {
							  this.$message({
								type: 'info',
								message: '已取消删除'
							  });          
							});
						}
					}
				}else if(this.pdfswitch){
					if(this.pdfanalysis.length<=0){
						this.$message.success('查询进度或下载文件请进入生成相应页面查看')
						this.pdfDownContent()
					}else{
						if(this.pdfanalysis[0].status!=0&&this.pdfanalysis[0].pdfNum<this.pdfanalysis[0].pdfTotal){
							this.$message.success('生成中，请勿重新点击，查询进度请前往下载成绩页面')
							return
						}
						if(this.pdfanalysis[0].status==1&&this.pdfanalysis[0].pdfNum>=this.pdfanalysis[0].pdfTotal){
							this.$confirm('已有PDF包，是否从新生成?', '提示', {
							  confirmButtonText: '确定',
							  cancelButtonText: '取消',
							  type: 'warning'
							}).then(() => {
								this.$message.success('查询进度或下载文件请进入生成相应页面查看')
								this.pdfDownContent()
							}).catch(() => {
							  this.$message({
								type: 'info',
								message: '已取消删除'
							  });          
							});
						}
					}
				}
			},
			//有痕成绩下载
			markdownContent(){
				let obj = {
				    navId: this.titlecontent.n5Id
				}
				markpaperafter(obj).then(res => {
				    if (res.code == 0) {
				        this.$message.success('查询进度或下载文件请进入生成相应页面查看')
						// this.loadIshow = false
				    }
				})
			},
			//pdf成绩下载
			pdfDownContent(){
				let obj = {
				    navId: this.titlecontent.n5Id
				}
				downloadPdfExamGenerate(obj).then(res => {
				    if (res.code == 0) {
				        this.$message.success('查询进度或下载文件请进入生成相应页面查看')
						// this.loadIshow = false
				    }
				})
			},
			//成绩分析下载
			downgetExamel(id){
				let obj = {
				    navId: this.titlecontent.n5Id,
				    type: 1,
				    genExcel: 1,
				    regen: id,//0开始 1重新
				    isCover: this.ykdbswitch ? 1 : 0,
				    isLine: this.onlineswitch ? 1 : 0,
				    lineType: 0
				}
				downloadExcel(obj).then(res => {
				    if (res.code == 0) {
						this.$message.success('成绩生成成功，请前往下载成绩页面下载')
						this.loadIshow = false
				    }
				})
			},
			fixecj(){
				this.$router.push('/Confirmmain/onlineinfo?con='+this.$route.query.con)
			},
			//生成成绩分析
			getExcelList() {
			  let query = {
			    pageIndex: this.pageIndex,
			    pageSize: this.pageSize,
			    navId: this.titlecontent.n5Id,
			  }
			  analysisStuScoreExcelDownloadPage(query).then(res => {
			    // console.log(res);
				if(res.code==0){
					this.excelanalysis = res.data.list
				}else{
					this.$message({
						type: 'error',
						message: res.msg
					}); 
				}
			  })
			},
			//生成pdf
			getPdflist(){
				let obj = {
					navId:this.titlecontent.n5Id,
					pageIndex:this.pageIndex,
					pageSize:this.pageSize
				}
				downloadPdfPage(obj).then(res=>{
					if(res.code==0){
						this.pdfanalysis = res.data
					}else{
						this.$message({
							type: 'error',
							message: res.msg
						}); 
					}
				})
			},
			//生成有痕阅卷
			getMarklist(){
				let params={
					navId:this.titlecontent.n5Id,
					pageIndex:this.pageIndex,
					pageSize:this.pageSize
				}
				navmarkpaperlist(params).then(res=>{
					if(res.code==0){
						this.markanalysis = res.data.list
						// this.listTotal = res.data.total
					}else{
						this.$message({
							type: 'error',
							message: res.msg
						});
					}
				})
			},
			onlineswitchButton(){
				let allhe = parseInt(this.online.wknum1+this.online.wknum2+this.online.lknum1+this.online.lknum2)
				if(allhe>0){
					this.onlineswitch = true
				}else{
					this.onlineswitch = false
					this.$message({
						type: 'error',
						message: '当前的上线人数小于0'
					})
				}
			}
		}
	}
</script>

<style scoped>
	.boxmain h3{
		padding:20px 0;
		font-size:18px;
		display: flex;
	}
	.boxmain{
		padding:30px;
	}
	.boxmain h3 span{
		display: inline-block;
		margin-left:20px;
		cursor: pointer;
		font-size:14px;
		font-weight:normal;
		color:#165DFF
	}
	.switchBox{
		display: flex;
		flex-wrap: wrap;
		align-items: center;
	}
	.switchBox li{
		display: flex;
		align-items: center;
		margin-right:50px;
	}
	.switchBox li span{
		display: inline-block;
		margin-right:30px;
	}
	.switchBox1{
		display: flex;
		flex-wrap: wrap;
		align-items: center;
	}
	.switchBox1 li{
		display: flex;
		align-items: center;
		margin-right:50px;
		margin-bottom:20px;
	}
	.switchBox1 li span{
		display: inline-block;
		margin-right:30px;
		width:200px;
	}
	.startsc{
		margin-top:150px;
		display: flex;
	}
</style>